<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/3/19
 * Time: 13:50
 * Desc: 商品分类
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use app\mall\model\GoodsCategory as GoodsCategoryModel;
use app\mall\service\GoodsService;

class Goodscategory extends BasicAdmin{
    /**
     * @Author  lingyun
     * @Desc    商品分类
     */
    public function index(){
        $this->title = '商品分类';
        $get = $this->request->get();
        $db = Db::name('goods_category')->where('is_deleted',0);
        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    添加 -- 编辑商品分类
     */
    public function add_category(){
        $categoryservice = new GoodsService();      //商品服务层
        if(request()->isPost()){
            $post = input('post.');
            $post['addtime'] = time();
            if(empty($post['id'])){     //添加
                $categoryservice->add_category($post);
            }else{
                $categoryservice->edit_category($post,$post['id']);
            }
        }else{
            $id = input('id');
            if(empty($id)){
                $list = GoodsCategoryModel::all(['status'=>1]);
                $this->assign('list',$list);
            }else{
                $list = Db::name('goods_category')->where('status',1)->where('pid',0)->where('id','neq',$id)->select();
                $this->assign('list',$list);
            }
            $vo = GoodsCategoryModel::get(['id'=>$id]);
            $yangshi = config('qiniu');
            $this->assign('yangshi',$yangshi);          //七牛云图片压缩样式
            $this->assign('vo',$vo);
            return $this->fetch('add_category');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    设置商品分类状态
     */
    public function setCateStatus(){
        $post = input('post.');
        //更新状态
        $res = Db::name('goods_category')->update(['id'=>$post['id'],'status'=>$post['value']]);
        $res!==false?$this->success('更新成功!',''):$this->error('更新失败','');
    }

    /**
     * @Author  lingyun
     * @Desc    设置推荐商品分类
     * @return mixed
     */
    public function set_recommend(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            if(empty($post['is_recommend'])){
                $this->error('请选择推荐位置','');
            }

            if($post['is_recommend'] == 3){
                $res = db('goods_category')->where('id',$post['id'])->update(['is_recommend'=>0]);
                $res!==false?$this->success('取消成功',''):$this->error('取消失败','');
            }
            $where = [
                'is_recommend'=>$post['is_recommend'],
                'id'=>array('neq',$post['id'])
            ];
            $count = db('goods_category')->where($where)->count();      //计算推荐的商品数量
            $msg = $post['is_recommend'] == 1?'推荐位一已经设置，请先取消再设置':'推荐位二已经存在，请先取消再设置';
            if($count == 1){
                $this->error($msg,'');
            }
            $res = db('goods_category')->update(['id'=>$post['id'],'is_recommend'=>$post['is_recommend']]);
            $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
        }

        $id = input('id');
        $info = db('goods_category')->where('id',$id)->find();      //商品分类

        $this->assign('id',$id);
        $this->assign('info',$info);

        return $this->fetch('set_recommend_category');
    }

    /**
     * @Author  lingyun
     * @Desc    添加推荐商品
     * 2018-30-14
     */
    public function add_recommend_goods(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $sign = db('goods_category')->where('id',$post['cate_id'])->find();
            $count = db('goods_recommend')->where('cate_id',$post['cate_id'])->count();

            if(!isset($post['id'])){                                //推荐商品
                $post['addtime'] = time();
                if($sign['is_recommend'] == 1){
                    if($count >= 3){
                        $this->error('最多允许推荐三个商品','');
                    }else{
                        $res = db('goods_recommend')->insert($post);
                        $res>0?$this->success('保存成功',''):$this->error('保存失败','');
                    }
                }else if($sign['is_recommend'] == 2){
                    if($count >= 4){
                        $this->error('最多允许推荐四个商品','');
                    }else{
                        $res = db('goods_recommend')->insert($post);
                        $res>0?$this->success('保存成功',''):$this->error('保存失败','');
                    }
                }
            }else{
                $res = db('goods_recommend')->update($post);
                $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
            }
//            $cate = db('goods_category')->where('id',$post['id'])->find();
        }else{
            $id = input('id');
            $cate_id = input('cate_id');        //推荐商品分类
            $info = db('goods_recommend')->where('id',$id)->find();
            $goods = db('goods')->where(['status'=>1,'is_deleted'=>0,'cat_id'=>$cate_id])->field('id,name,logo')->select();

            $this->assign('id',$id);
            $this->assign('info',$info);
            $this->assign('goods',$goods);
            $this->assign('cate_id',$cate_id);

            return $this->fetch('add_recommend_goods');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    设置推荐商品
     * @return mixed
     */
    public function set_recommend_goods(){
        $id = input('id');
        $title = input('title');
        $info = db('goods_category')->where('id',$id)->find();        //推荐位置
        $map['a.cate_id'] = array('eq',$id);
        $map['a.is_deleted'] = array('eq',0);
        $where = [
            'a.cate_id'=>$id,
            'a.is_deleted'=>0
        ];

        $goods = db('goods_recommend')->alias('a')->join('goods b','a.goods_id=b.id','LEFT')->where($where)->field('a.*,b.name,b.logo')->select();                     //推荐的商品

        $this->assign('info',$info);
        $this->assign('list',$goods);
        $this->assign('id',$id);
        $qiniu = config('qiniu');
        $this->assign('yangshi', $qiniu['yangshi']);          //七牛样式后缀
        $this->assign('title','推荐商品');          //七牛样式后缀

        return $this->fetch('recommend_goods');
    }

    /**
     * @Author  lingyun
     * @Desc    删除推荐商品
     */
    public function del_recomend_goods(){
        $id = input('id');
        $res = db('goods_recommend')->where('id','in',$id)->delete();
        $res>0?$this->success('删除成功',''):$this->error('删除失败','');
    }

    /**
     * @Author  lingyun
     * @Desc    删除商品分类
     */
    public function del_category(){
        $post = input('post.');
        $res = Db::name('goods_category')->where('id','in',$post['id'])->update(['is_deleted'=>1]);
        $res!==false?$this->success('删除成功!',''):$this->error('删除失败!','');
    }

    /**
     * @Author  lingyun
     * @Desc    更新商品分类排序
     */
    public function changeCateDis0rder(){
        $post = input('post.');
        $res = Db::name('goods_category')->update($post);
        $res!==false?$this->success('更新成功',''):$this->error('更新失败','');
    }


}